package vn.isolar.main;

import java.io.File;
import java.io.IOException;

import org.apache.http.client.ClientProtocolException;

import test.vn.R;
import vn.isolar.NLP.AnswerGenerate;
import vn.isolar.dialog.DialogManager;
import vn.isolar.io.TomCatClient;
import vn.isolar.object.Command;
import vn.isolar.object.Executable;
import vn.isolar.system.audio.Recorder;
import vn.isolar.system.service.ServiceTrackLocation;
import vn.isolar.system.timer.ScheduleTask;
import vn.isolar.util.direction.Direction;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SvntestActivity extends Activity {
    /** Called when the activity is first created. */
	/*
	 * Declare variable for GUI
	 */
	private Button btn_record;
	private TextView tv_result;
	//
	/*
	 * Declare global variable
	 */
	private File extStore;			// Store sdcard path
	public static TomCatClient tomClient; // Connect to server
	private Command cmd;
	DialogManager dialogManager;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initGlobal();
        initButton();
        
        
    }
    private void initGlobal(){
    	 extStore = Environment.getExternalStorageDirectory();
    	 tomClient = new TomCatClient(this);
    }
    /*--------------------------------------
     * Initial GUI factors here
     */
    private void initButton(){
    	btn_record = (Button)findViewById(R.id.btn_record);
    	btn_record.setOnClickListener(btnClick);
    	tv_result = (TextView)findViewById(R.id.textView1);
    }
    /*-------------------------------------
     * Declare action for GUI factors here  
     */
    private View.OnClickListener btnClick = new View.OnClickListener() {
    	
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.btn_record: {
					startService(new Intent(SvntestActivity.this,ServiceTrackLocation.class));
					new TaskCommand().execute();
					
					//Toast t = null;
					//t.makeText(getApplicationContext(), "start service", Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}
	};
	
	/*
	 * Function to display the result to screen,
	 * 
	 */
	private void displayResult(TextView tv,String text){
		tv.setText(text);
	}
	/*
	 * Asyntask recording audio
	 */
	private class TaskCommand extends AsyncTask<Void, Void, String>{
		private Recorder rec;
		private ProgressDialog progressDialog;
		private String result_text = "";
		/*
		 * After recording finish, do something here
		 */
		@Override
		protected void onPostExecute(String result_text) {
			//finish recording, disable dialog
			
			if (progressDialog.isShowing())
				progressDialog.dismiss();
			
			
			//Display result on screen
			displayResult(tv_result, result_text);
			
			cmd = new Command(result_text);
			Executable exe = cmd.createExe();
			if(exe.getType() == "GET_LOCATION"){		//If command is requet location
				exe.execute(SvntestActivity.this);
				/*
				 * note: Request Location is special so 
				 * request TTS has been already implement on execute() function. 
				 * No need to call it again
				 */
				 
			}
			else{
				String textTTS = new AnswerGenerate(cmd).generate();
				startActivity(exe.createIntent());
				//Request TTS and play audio file
				tomClient.execute(textTTS);
				
			}
		}

		@Override
		protected void onPreExecute() {
			//Show progress dialog
			progressDialog = new ProgressDialog(SvntestActivity.this);
			progressDialog.show();
			
			// Uncomment if want to record from microphone
			rec = new Recorder(); 
		}

		@Override
		protected String doInBackground(Void... arg0) {
			// Uncomment if want to record from microphone
			String fileAudio = rec.startRecord();
			//return filepath;
			
			//return extStore.getPath() + "/audio2.wav";
			//return "audio.wav";
			//After record, send to server
			//String fileAudio=extStore.getPath() + "/audio21.wav";
			try {
				
				String fileXml = tomClient.createXML("ASR", "");
				//String fileXml = tomClient.createXML("TTS", "fdsfasd");
				result_text = tomClient.execute(fileXml, fileAudio);
				//result_text = tomClient.execute(fileXml, "");
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}// Create command from result
			return result_text;

		}

		
	}
}